package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.good.result.DeleteCartGoodsParam;
import com.jy.entity.Cart;
import com.jy.entity.CartGoods;
import com.jy.entity.GoodsStyle;
import com.jy.entity.User;
import com.jy.mapper.CartGoodsMapper;
import com.jy.mapper.CartMapper;
import com.jy.mapper.GoodsStyleMapper;
import com.jy.mapper.UserMapper;
import com.jy.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 购物车表 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-18
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    private final CartMapper cartMapper;

    private final UserMapper userMapper;

    private final GoodsStyleMapper goodsStyleMapper;
    private final CartGoodsMapper cartGoodsMapper;

    public CartServiceImpl(CartMapper cartMapper, UserMapper userMapper, GoodsStyleMapper goodsStyleMapper, CartGoodsMapper cartGoodsMapper) {
        this.cartMapper = cartMapper;
        this.userMapper = userMapper;
        this.goodsStyleMapper = goodsStyleMapper;
        this.cartGoodsMapper = cartGoodsMapper;
    }

    @Override
    public ResponseWrapper getMyCartInfo(Integer userAidByToken) {

        HashMap<String, Object> result = new HashMap<>();

        List<Cart> carts = cartMapper.selectList(new LambdaQueryWrapper<Cart>()
                .eq(Cart::getUserAid, userAidByToken)
        );
        // 查询用户的积分
        User user = userMapper.selectById(userAidByToken);
        result.put("recordNum", user.getRecords());
        if (carts == null || carts.isEmpty()) {
            result.put("cartGoods", null);
        }else {
            result.put("cartGoods", cartMapper.GetCurrentUserCartInfo(Long.valueOf(userAidByToken)));
        }
        return ResponseWrapper.markSuccess(result);
    }

    @Override
    public ResponseWrapper deleteCartGoods(List<DeleteCartGoodsParam> params) {

        params.forEach(param -> {
            GoodsStyle goodsStyle = goodsStyleMapper.selectOne(new LambdaQueryWrapper<GoodsStyle>()
                    .eq(GoodsStyle::getGoodAid, param.getGoodAid())
                    .eq(GoodsStyle::getStyleValue, param.getSelectStyle())
            );
            if (goodsStyle != null) {
                int delete = cartGoodsMapper.delete(new LambdaQueryWrapper<CartGoods>()
                        .eq(CartGoods::getCartAid, param.getCartAid())
                        .eq(CartGoods::getGoodsAid, param.getGoodAid())
                        .eq(CartGoods::getGoodsStyleAid, goodsStyle.getAid())
                );
                Cart cart = cartMapper.selectById(param.getCartAid());
                cart.setUpdateTime(LocalDateTime.now());
                cart.setGoodsNum(cart.getGoodsNum() - delete);
                cartMapper.updateById(cart);
            }
        });

        return ResponseWrapper.markSuccess();
    }
}
